/*   1:    */
package com.course.utils.binary;
/*   2:    */
/*   3:    */

import java.util.Arrays;

/*   8:    */
/*   9:    */ public abstract class BaseNCodec
        /*  10:    */ implements BinaryEncoder, BinaryDecoder
        /*  11:    */ {
    /*  12:    */   static final int EOF = -1;
    /*  13:    */   public static final int MIME_CHUNK_SIZE = 76;
    /*  14:    */   public static final int PEM_CHUNK_SIZE = 64;
    /*  15:    */   private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;
    /*  16:    */   private static final int DEFAULT_BUFFER_SIZE = 8192;
    /*  17:    */   protected static final int MASK_8BITS = 255;
    /*  18:    */   protected static final byte PAD_DEFAULT = 61;

    /*  19:    */
    /*  20:    */   static class Context
            /*  21:    */ {
        /*  22:    */ int ibitWorkArea;
        /*  23:    */ long lbitWorkArea;
        /*  24:    */ byte[] buffer;
        /*  25:    */ int pos;
        /*  26:    */ int readPos;
        /*  27:    */ boolean eof;
        /*  28:    */ int currentLinePos;
        /*  29:    */ int modulus;

        /*  30:    */
        /*  31:    */
        public String toString()
        /*  32:    */ {
            /*  33:103 */
            return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, modulus=%s, pos=%s, readPos=%s]", new Object[]{getClass().getSimpleName(), Arrays.toString(this.buffer), Integer.valueOf(this.currentLinePos), Boolean.valueOf(this.eof), Integer.valueOf(this.ibitWorkArea), Long.valueOf(this.lbitWorkArea), Integer.valueOf(this.modulus), Integer.valueOf(this.pos), Integer.valueOf(this.readPos)});
            /*  34:    */
        }
        /*  35:    */
    }

    /*  36:    */
    /*  37:156 */   protected final byte PAD = 61;
    /*  38:    */   private final int unencodedBlockSize;
    /*  39:    */   private final int encodedBlockSize;
    /*  40:    */   protected final int lineLength;
    /*  41:    */   private final int chunkSeparatorLength;

    /*  42:    */
    /*  43:    */
    protected BaseNCodec(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength)
    /*  44:    */ {
        /*  45:186 */
        this.unencodedBlockSize = unencodedBlockSize;
        /*  46:187 */
        this.encodedBlockSize = encodedBlockSize;
        /*  47:188 */
        boolean useChunking = (lineLength > 0) && (chunkSeparatorLength > 0);
        /*  48:189 */
        this.lineLength = (useChunking ? lineLength / encodedBlockSize * encodedBlockSize : 0);
        /*  49:190 */
        this.chunkSeparatorLength = chunkSeparatorLength;
        /*  50:    */
    }

    /*  51:    */
    /*  52:    */   boolean hasData(Context context)
    /*  53:    */ {
        /*  54:200 */
        return context.buffer != null;
        /*  55:    */
    }

    /*  56:    */
    /*  57:    */   int available(Context context)
    /*  58:    */ {
        /*  59:210 */
        return context.buffer != null ? context.pos - context.readPos : 0;
        /*  60:    */
    }

    /*  61:    */
    /*  62:    */
    protected int getDefaultBufferSize()
    /*  63:    */ {
        /*  64:219 */
        return 8192;
        /*  65:    */
    }

    /*  66:    */
    /*  67:    */
    private byte[] resizeBuffer(Context context)
    /*  68:    */ {
        /*  69:227 */
        if (context.buffer == null)
            /*  70:    */ {
            /*  71:228 */
            context.buffer = new byte[getDefaultBufferSize()];
            /*  72:229 */
            context.pos = 0;
            /*  73:230 */
            context.readPos = 0;
            /*  74:    */
        }
        /*  75:    */
        else
            /*  76:    */ {
            /*  77:232 */
            byte[] b = new byte[context.buffer.length * 2];
            /*  78:233 */
            System.arraycopy(context.buffer, 0, b, 0, context.buffer.length);
            /*  79:234 */
            context.buffer = b;
            /*  80:    */
        }
        /*  81:236 */
        return context.buffer;
        /*  82:    */
    }

    /*  83:    */
    /*  84:    */
    protected byte[] ensureBufferSize(int size, Context context)
    /*  85:    */ {
        /*  86:246 */
        if ((context.buffer == null) || (context.buffer.length < context.pos + size)) {
            /*  87:247 */
            return resizeBuffer(context);
            /*  88:    */
        }
        /*  89:249 */
        return context.buffer;
        /*  90:    */
    }

    /*  91:    */
    /*  92:    */   int readResults(byte[] b, int bPos, int bAvail, Context context)
    /*  93:    */ {
        /*  94:269 */
        if (context.buffer != null)
            /*  95:    */ {
            /*  96:270 */
            int len = Math.min(available(context), bAvail);
            /*  97:271 */
            System.arraycopy(context.buffer, context.readPos, b, bPos, len);
            /*  98:272 */
            context.readPos += len;
            /*  99:273 */
            if (context.readPos >= context.pos) {
                /* 100:274 */
                context.buffer = null;
                /* 101:    */
            }
            /* 102:276 */
            return len;
            /* 103:    */
        }
        /* 104:278 */
        return context.eof ? -1 : 0;
        /* 105:    */
    }

    /* 106:    */
    /* 107:    */
    protected static boolean isWhiteSpace(byte byteToCheck)
    /* 108:    */ {
        /* 109:289 */
        switch (byteToCheck)
            /* 110:    */ {
            /* 111:    */
            case 9:
                /* 112:    */
            case 10:
                /* 113:    */
            case 13:
                /* 114:    */
            case 32:
                /* 115:294 */
                return true;
            /* 116:    */
        }
        /* 117:296 */
        return false;
        /* 118:    */
    }

    /* 119:    */
    /* 120:    */
    public Object encode(Object obj)
    /* 121:    */     throws EncoderException
    /* 122:    */ {
        /* 123:312 */
        if (!(obj instanceof byte[])) {
            /* 124:313 */
            throw new EncoderException("Parameter supplied to Base-N encode is not a byte[]");
            /* 125:    */
        }
        /* 126:315 */
        return encode((byte[]) obj);
        /* 127:    */
    }

    /* 128:    */
    /* 129:    */
    public String encodeToString(byte[] pArray)
    /* 130:    */ {
        /* 131:327 */
        return StringUtils.newStringUtf8(encode(pArray));
        /* 132:    */
    }

    /* 133:    */
    /* 134:    */
    public String encodeAsString(byte[] pArray)
    /* 135:    */ {
        /* 136:338 */
        return StringUtils.newStringUtf8(encode(pArray));
        /* 137:    */
    }

    /* 138:    */
    /* 139:    */
    public Object decode(Object obj)
    /* 140:    */     throws DecoderException
    /* 141:    */ {
        /* 142:354 */
        if ((obj instanceof byte[])) {
            /* 143:355 */
            return decode((byte[]) obj);
            /* 144:    */
        }
        /* 145:356 */
        if ((obj instanceof String)) {
            /* 146:357 */
            return decode((String) obj);
            /* 147:    */
        }
        /* 148:359 */
        throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
        /* 149:    */
    }

    /* 150:    */
    /* 151:    */
    public byte[] decode(String pArray)
    /* 152:    */ {
        /* 153:371 */
        return decode(StringUtils.getBytesUtf8(pArray));
        /* 154:    */
    }

    /* 155:    */
    /* 156:    */
    public byte[] decode(byte[] pArray)
    /* 157:    */ {
        /* 158:383 */
        if ((pArray == null) || (pArray.length == 0)) {
            /* 159:384 */
            return pArray;
            /* 160:    */
        }
        /* 161:386 */
        Context context = new Context();
        /* 162:387 */
        decode(pArray, 0, pArray.length, context);
        /* 163:388 */
        decode(pArray, 0, -1, context);
        /* 164:389 */
        byte[] result = new byte[context.pos];
        /* 165:390 */
        readResults(result, 0, result.length, context);
        /* 166:391 */
        return result;
        /* 167:    */
    }

    /* 168:    */
    /* 169:    */
    public byte[] encode(byte[] pArray)
    /* 170:    */ {
        /* 171:403 */
        if ((pArray == null) || (pArray.length == 0)) {
            /* 172:404 */
            return pArray;
            /* 173:    */
        }
        /* 174:406 */
        Context context = new Context();
        /* 175:407 */
        encode(pArray, 0, pArray.length, context);
        /* 176:408 */
        encode(pArray, 0, -1, context);
        /* 177:409 */
        byte[] buf = new byte[context.pos - context.readPos];
        /* 178:410 */
        readResults(buf, 0, buf.length, context);
        /* 179:411 */
        return buf;
        /* 180:    */
    }

    /* 181:    */
    /* 182:    */
    abstract void encode(byte[] paramArrayOfByte, int paramInt1, int paramInt2, Context paramContext);

    /* 183:    */
    /* 184:    */
    abstract void decode(byte[] paramArrayOfByte, int paramInt1, int paramInt2, Context paramContext);

    /* 185:    */
    /* 186:    */
    protected abstract boolean isInAlphabet(byte paramByte);

    /* 187:    */
    /* 188:    */
    public boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad)
    /* 189:    */ {
        /* 190:441 */
        for (int i = 0; i < arrayOctet.length; i++) {
            /* 191:442 */
            if ((!isInAlphabet(arrayOctet[i])) && ((!allowWSPad) || ((arrayOctet[i] != 61) && (!isWhiteSpace(arrayOctet[i]))))) {
                /* 192:444 */
                return false;
                /* 193:    */
            }
            /* 194:    */
        }
        /* 195:447 */
        return true;
        /* 196:    */
    }

    /* 197:    */
    /* 198:    */
    public boolean isInAlphabet(String basen)
    /* 199:    */ {
        /* 200:460 */
        return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
        /* 201:    */
    }

    /* 202:    */
    /* 203:    */
    protected boolean containsAlphabetOrPad(byte[] arrayOctet)
    /* 204:    */ {
        /* 205:473 */
        if (arrayOctet == null) {
            /* 206:474 */
            return false;
            /* 207:    */
        }
        /* 208:476 */
        for (byte element : arrayOctet) {
            /* 209:477 */
            if ((61 == element) || (isInAlphabet(element))) {
                /* 210:478 */
                return true;
                /* 211:    */
            }
            /* 212:    */
        }
        /* 213:481 */
        return false;
        /* 214:    */
    }

    /* 215:    */
    /* 216:    */
    public long getEncodedLength(byte[] pArray)
    /* 217:    */ {
        /* 218:495 */
        long len = (pArray.length + this.unencodedBlockSize - 1) / this.unencodedBlockSize * this.encodedBlockSize;
        /* 219:496 */
        if (this.lineLength > 0) {
            /* 220:498 */
            len += (len + this.lineLength - 1L) / this.lineLength * this.chunkSeparatorLength;
            /* 221:    */
        }
        /* 222:500 */
        return len;
        /* 223:    */
    }
    /* 224:    */
}



/* Location:           C:\Users\Fenix\Downloads\commons-codec-1.8.jar

 * Qualified Name:     org.apache.commons.codec.binary.BaseNCodec

 * JD-Core Version:    0.7.0.1

 */